package cn.com.bluemoon.daps.sync.entity;

import cn.com.bluemoon.daps.common.domain.BaseModel;
import cn.com.bluemoon.daps.common.enums.SyncStatus;
import cn.com.bluemoon.daps.common.enums.SyncToEnv;
import com.baomidou.mybatisplus.annotation.EnumValue;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;

import java.time.LocalDateTime;

/**
 * <p>
 * 开发同步库表任务表
 * </p>
 *
 * @author admin
 * @since 2021-06-17
 */
@Data
@Accessors
@ApiModel(value = "DapSyncDevEnvTask", description = "开发同步库表任务表")
public class DapSyncDevEnvTask extends BaseModel<DapSyncDevEnvTask> {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "模型id")
    private String modelId;

    @ApiModelProperty(value = "模型表id")
    private String tableId;

    @ApiModelProperty(value = "任务类型，对应1：开发库同步、2：测试库同步")
    private SyncToEnv syncEnv;

    @ApiModelProperty(value = "ddl语句")
    private String syncDdl;

    @ApiModelProperty(value = "pt工具语句")
    private String syncPtsql;

    @ApiModelProperty(value = "同步状态，0：未同步、1：同步中、2：已同步、3：同步失败，若模型数据源中不存在该环境则不进行同步")
    @EnumValue
    private SyncStatus syncStatus;

    @ApiModelProperty(value = "同步执行人")
    private String syncExecBy;

    @ApiModelProperty(value = "同步开始时间")
    @JsonFormat(
            pattern = "yyyy-MM-dd HH:mm:ss",
            timezone = "GMT+8"
    )
    private LocalDateTime syncStartTime;

    @ApiModelProperty(value = "同步结束时间")
    @JsonFormat(
            pattern = "yyyy-MM-dd HH:mm:ss",
            timezone = "GMT+8"
    )
    private LocalDateTime syncEndTime;

    @ApiModelProperty(value = "同步日志")
    private String syncLog;

    @ApiModelProperty(value = "记录该任务所属数据源id")
    private String dataSourceId;

    @ApiModelProperty(value = "记录日志文件路径，用于任务比较长时间处于同步中的，可以通过ssh访问该路径进行获取最新日志")
    private String syncLogPath;

    @ApiModelProperty(value = "执行脚本的完整语句")
    private String syncScript;

    @ApiModelProperty(value = "执行脚本前的建表语句")
    private String syncPreTableCreateSql;


    /**
     * 获取最新行同步时间
     *
     * @param priorityEnd 优先拿结束时间，不管是否为null
     * @return time
     */
    public LocalDateTime _getSyncTime(boolean priorityEnd) {
        if (priorityEnd) {
            return getSyncEndTime();
        }
        return getSyncEndTime() == null ? getSyncStartTime() : getSyncEndTime();
    }


}
